#include "SList.h"



void SLTPintf(SLTNode* phead)
{
	SLTNode* cur = phead;
	while (cur != NULL)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

SLTNode* AppSLTnext(SLDataType x)
{
	SLTNode* nextnode = (SLTNode*)malloc(sizeof(SLTNode));
	if (nextnode == NULL)
	{
		perror("malloc fail");
		return NULL;
	}

	nextnode->data = x;
	nextnode->next = NULL;
	return nextnode;
}



void SLTPushBack(SLTNode** pphead, SLDataType x)
{
	SLTNode* nextnode = AppSLTnext(x);

	if (NULL == *pphead)
	{
		*pphead = nextnode;
	}
	else
	{
		SLTNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = nextnode;
	}
}



void SLTPushFront(SLTNode** pphead, SLDataType x)
{
	SLTNode* nextnode = AppSLTnext(x);
	nextnode->next = *pphead;
	*pphead = nextnode;
}



void SLTPopBack(SLTNode** pphead)
{
	assert(*pphead);

	if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		SLTNode* tail = *pphead;
		while (tail->next->next != NULL)
		{
			tail = tail->next;
		}
		free(tail->next);
		tail->next = NULL;
	}
}



void SLTPopFront(SLTNode** pphead)
{
	assert(*pphead);

	SLTNode* first = *pphead;

	*pphead = first->next;
	free(first);
	first = NULL;


}